//
//  HD_HSP_Map_ViewController.m
//  HDLiaoNingMeseum
//
//  Created by HDHaoShaoPeng on 2017/8/4.
//  Copyright © 2017年 HDNiuKuiming. All rights reserved.
//

#import "HD_HSP_Map_ViewController.h"
#import "HDMapView.h"
#import "UIView+Extension.h"
#import "HDDeclare.h"
#import "HD_HSP_Exhibit_Model.h"
#import "HD_HSP_FloorImage_ViewController.h"
#import "HD_HSP_Player_ViewController.h"


@interface HD_HSP_Map_ViewController ()<HDMapViewDataSource,HDMapViewDelegate,UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UIView *mapBottomView;
@property (weak, nonatomic) IBOutlet UITableView *myTableView;
@property (weak, nonatomic) IBOutlet UIButton *rightButton;

@property (weak, nonatomic) IBOutlet UILabel *myTItleLabel;
@property(weak,nonatomic)HDDeclare *declare;
@property(strong,nonatomic)HDMapView *myMapView;
@end

static HDDeclare *declare;
@implementation HD_HSP_Map_ViewController
{
    int _floorNum;
    BOOL _nowMode;
    NSArray *_dataArr;
    HD_HSP_Exhibit_Model *_currentModel;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    declare = [HDDeclare sharedDeclare];
    
    
    self.myTItleLabel.text = declare.mapGuide;
    self.myTItleLabel.font = [UIFont fontWithName:@"FZQKBYSJW--GB1-0" size:18.0];
    
    // Do any additional setup after loading the view.
    _nowMode = YES;
    _floorNum = 1;
    
    NSArray *tingsArr = [self.declare giveMeZhanTingData];
    NSMutableArray *oneFloorArr = [NSMutableArray arrayWithCapacity:20];
    NSMutableArray *towFloorArr = [NSMutableArray arrayWithCapacity:20];
    NSMutableArray *threeFloorArr = [NSMutableArray arrayWithCapacity:20];
    for (HD_HSP_Exhibit_Model *model in tingsArr) {
        if (model.tingFloor == 1) {
            [oneFloorArr addObject:model];
        }
        else if (model.tingFloor == 2) {
            [towFloorArr addObject:model];
        }
        else if (model.tingFloor == 3) {
            [threeFloorArr addObject:model];
        }
    }
    _dataArr = @[oneFloorArr,towFloorArr,threeFloorArr];
}

-(void)viewDidAppear:(BOOL)animated
{
    if (!_myMapView) {
        [self.mapBottomView addSubview:self.myMapView];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
    if ([segue.identifier isEqualToString:@"showFloorImageLine"]) {
        HD_HSP_FloorImage_ViewController *vc = segue.destinationViewController;
        vc.Floor = _floorNum;
    }
}


#pragma mark - 点击方法
- (IBAction)showChooseTableOrNot:(id)sender {
    self.myTableView.hidden = !self.myTableView.isHidden;
    if (_nowMode) {
        [self.rightButton setImage:[UIImage imageNamed:@"mapTopImage1"] forState:UIControlStateNormal];
    }
    else
    {
        [self.rightButton setImage:[UIImage imageNamed:@"mapTopImage"] forState:UIControlStateNormal];
    }
    
    _nowMode = !_nowMode;
}

- (IBAction)chooseFloorClicked:(id)sender {
    
}

- (IBAction)showRoadClicked:(id)sender {
    NSString *path = [NSString stringWithFormat:@"%@/map/%d/road.png",self.declare.catch_path,_floorNum];
    [self.myMapView myRouteImage:path];
}

- (IBAction)backClicked:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}


#pragma mark - 懒加载
-(HDMapView *)myMapView
{
    if (!_myMapView) {
        CGSize mapSize = CGSizeMake(465, 320);
        if (_currentModel) {
            mapSize = CGSizeMake(_currentModel.width/8.0, _currentModel.height/8.0);
        }
        
        NSLog(@"原始数据：%.2lf ppp %.2lf  新地图的contentSize:%@",_currentModel.width,_currentModel.height,NSStringFromCGSize(mapSize));
        
        _myMapView = [[HDMapView alloc] initWithFrame:self.mapBottomView.bounds contentSize:mapSize];
        _myMapView.dataSource = self;
        _myMapView.mapViewdelegate = self;
        _myMapView.levelsOfZoom = 2;
        _myMapView.levelsOfDetail = 2;
        
        [self addAnnatation];
    }
    return _myMapView;
}

-(HDDeclare *)declare
{
    if (!_declare) {
        _declare = [HDDeclare sharedDeclare];
    }
    return _declare;
}


#pragma mark - mapView delegate
- (void)mapView:(HDMapView *)mapView didReceiveSingleTap:(UIGestureRecognizer *)gestureRecognizer
{
    [self.myMapView allAnnShowNormal];
}

- (void)mapView:(HDMapView *)mapView didReceiveDoubleTap:(UIGestureRecognizer *)gestureRecognizer
{
    [self.myMapView allAnnShowNormal];
}

#pragma mark - mapViewSource delegate

-(void)annClickedWith:(HDAnnotation *)ann
{
    HD_HSP_Exhibit_Model *model = [[HD_HSP_Exhibit_Model alloc]init];
    model.FileNum = ann.identify;
    
    HD_HSP_Player_ViewController *firVC = [[UIStoryboard storyboardWithName:@"HD_HSP_Data" bundle:nil] instantiateViewControllerWithIdentifier:@"HD_HSP_Player_ViewController"];
    firVC.detailItem = model;
    [self.navigationController pushViewController:firVC animated:YES];
}

-(void)addAnnatation
{
    NSArray *tempArr = _dataArr[0];
    for (HD_HSP_Exhibit_Model *model in tempArr) {
        
        model.X = [NSString stringWithFormat:@"%lf",model.width/2.0];
        model.Y = [NSString stringWithFormat:@"%lf",model.height/2.0];
        
        if ([model.X isKindOfClass:[NSNull class]] || [model.Y isKindOfClass:[NSNull class]]) {
            continue;
        }
        
        if (model.X.length < 1 || model.Y.length < 1 ) {
            continue;
        }
        
        float bili = 8.0;
        float x = model.X.floatValue/bili;
        float y = model.Y.floatValue/bili;
        
        //        x -= 75;
        //        y += 25;
        
        HDAnnotation *ann = [HDAnnotation annotationWithPoint:CGPointMake(x, y)];
        ann.identify = model.FileNum;
//        ann.imageUrl = model.map_icon;
//        ann.autoNum = model.autonum;
        
        
        
        [self.myMapView addAnnotation:ann animated:YES];
    }
    
    
    [self.myMapView setZoomScale:2.0 animated:YES];
}


- (UIImage *)mapView:(HDMapView *)mapView
         imageForRow:(NSInteger)row
              column:(NSInteger)column
               scale:(NSInteger)scale
{
    NSString *folderName = @"125";
    if (scale == 2)
        folderName = @"250";
    else if (scale == 4)
        folderName = @"500";
    else if (scale == 8)
        folderName = @"1000";
    
    
    
    NSString *name = [NSString stringWithFormat:@"%@/map/%d/%@/%ld_%ld.png",self.declare.catch_path,_floorNum,folderName,(long)column, (long)row];
    UIImage *myImage = [UIImage imageWithContentsOfFile:name];
    
    
    
    return myImage;
}

#pragma mark - tableView delegate

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30.0;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    float margin = 20.0;
    UIView *sectionView = [[UIView alloc]initWithFrame:CGRectMake(margin, 0, self.view.frame.size.width, 30.0)];
    sectionView.backgroundColor = [UIColor clearColor];
//    sectionView.tag = 100 + section;
    
    
//    UIImageView *bgImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, sectionView.frame.size.width, sectionView.frame.size.height)];
//    [sectionView addSubview:bgImageView];
//    [bgImageView setImage:[UIImage imageNamed:@"表头背景"]];
    
    
    UILabel *sectionLabel = [[UILabel alloc] init];
    [sectionView addSubview:sectionLabel];
    sectionLabel.frame = CGRectMake(10, 0, 120, 50.0);
    sectionLabel.textColor = [UIColor whiteColor];
    sectionLabel.text = [NSString stringWithFormat:@"组%ld",(long)section];
    sectionLabel.userInteractionEnabled = YES;
    sectionLabel.backgroundColor = [UIColor clearColor];
    sectionLabel.font = [UIFont fontWithName:@"FZQKBYSJW--GB1-0" size:13.0];
    
    
//    UIImage *arrowImage = [UIImage imageNamed:@"下箭头"];
//    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(sectionView.frame.size.width - 10 - arrowImage.size.width, (sectionHeight - arrowImage.size.height)/2.0, arrowImage.size.width, arrowImage.size.height)];
//    [sectionView addSubview:imageView];
//    imageView.image = arrowImage;
//    [_arrows addObject:imageView];
//    
//    
//    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(sectionClick:)];
//    [sectionView addGestureRecognizer:tap];
    
    
    return sectionView;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (_dataArr) {
        return _dataArr.count;
    }
    else
    {
        return 0;
    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (_dataArr) {
        NSArray *arr = _dataArr[section];
        return arr.count;
    }
    else
    {
        return 0;
    }
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    
    NSArray *tempArr = _dataArr[indexPath.section];
    HD_HSP_Exhibit_Model *model = tempArr[indexPath.row];
    cell.textLabel.text = model.c_subname;
    cell.textLabel.font = [UIFont fontWithName:@"FZQKBYSJW--GB1-0" size:13.0];
    cell.backgroundColor = [UIColor blackColor];
    cell.textLabel.textColor = [UIColor whiteColor];
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSArray *tempArr = _dataArr[indexPath.section];
    HD_HSP_Exhibit_Model *model = tempArr[indexPath.row];
    _floorNum = model.type;
    _currentModel = model;
    [self showChooseTableOrNot:nil];
    [self.myMapView removeFromSuperview];
    self.myMapView = nil;
    [self.mapBottomView addSubview:self.myMapView];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 30.0;
}

//-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
//{   //if (!indexPath.section) return UITableViewCellEditingStyleNone; // 第一组家和公司不提供左滑删除功能
//    return UITableViewCellEditingStyleDelete;
//}
//
//- (void)tableView:(UITableView*)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath*)indexPath {
//    if (editingStyle == UITableViewCellEditingStyleDelete) {
//        
//        //[self.da removeObjectAtIndex:indexPath.row];
//        [self.myTableView deleteRowsAtIndexPaths:@[indexPath]
//                                withRowAnimation:UITableViewRowAnimationAutomatic];
//        [self.myTableView reloadData];
//    }
//}

//-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    return YES;
//}


@end
